<?php

namespace App\Observers;

use App\Handlers\KeysHandler;
use App\Models\GoodsOrder;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;

class GoodsOrderObserver
{

    public function saved(GoodsOrder $goodsOrder)
    {
        if(isset($goodsOrder->user_id)){
            $nowdatestr = Carbon::now()->format('Ymd');
            Cache::forget(sprintf(KeysHandler::SHOP_STATISTICS_ORDER, 'day'.$nowdatestr, $goodsOrder->user_id));
            Cache::forget(sprintf(KeysHandler::SHOP_STATISTICS_ORDER, 'week'.$nowdatestr, $goodsOrder->user_id));
            Cache::forget(sprintf(KeysHandler::SHOP_STATISTICS_ORDER, 'month'.$nowdatestr, $goodsOrder->user_id));
            Cache::forget(sprintf(KeysHandler::SHOP_STATISTICS_ORDER, 'all'.$nowdatestr, $goodsOrder->user_id));
        }

    }

    public function deleted(GoodsOrder $goodsOrder)
    {
        if(isset($goodsOrder->user_id)){
            $nowdatestr = Carbon::now()->format('Ymd');
            Cache::forget(sprintf(KeysHandler::SHOP_STATISTICS_ORDER, 'day'.$nowdatestr, $goodsOrder->user_id));
            Cache::forget(sprintf(KeysHandler::SHOP_STATISTICS_ORDER, 'week'.$nowdatestr, $goodsOrder->user_id));
            Cache::forget(sprintf(KeysHandler::SHOP_STATISTICS_ORDER, 'month'.$nowdatestr, $goodsOrder->user_id));
            Cache::forget(sprintf(KeysHandler::SHOP_STATISTICS_ORDER, 'all'.$nowdatestr, $goodsOrder->user_id));
        }
    }
}
